package com.itheima;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
public class PoiDemo {

    /**
     * 读取excel的文件
     *
     * @param args
     */
    public static void main1(String[] args) throws IOException {
        //创建工作薄对象（excel）
        XSSFWorkbook workbook = new XSSFWorkbook("C:\\Users\\sun\\Desktop\\test.xlsx");
        //获取工作表对象
        XSSFSheet sheet = workbook.getSheetAt(0);
        //获取行对象
//        XSSFRow row = sheet.getRow(0);
//        System.out.println(row);
        //获取单元格
//        XSSFCell cell = row.getCell(0);
//        System.out.println(cell);
        //最后一行的行号
        int lastRowNum = sheet.getLastRowNum();
//        System.out.println(lastRowNum);
        for (int i = 0; i <= lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            //该行有多少个单元格
            short lastCellNum = row.getLastCellNum();
//            System.out.println(lastCellNum);
            for (int j = 0; j < lastCellNum; j++) {
                XSSFCell cell = row.getCell(j);

                //获取单元格的类型
                int cellType = cell.getCellType();
                if(cellType == Cell.CELL_TYPE_STRING){
//                    System.out.println("字符串类型");
                    System.out.println(cell);
                }else if (cellType == Cell.CELL_TYPE_NUMERIC){
                    //可以把数值类型单元格作为字符串类型处理
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    System.out.println(cell);
                }




            }
        }
    }

    public static void main2(String[] args) throws IOException {
        //创建工作薄对象（excel）
        XSSFWorkbook workbook = new XSSFWorkbook("C:\\Users\\sun\\Desktop\\test.xlsx");
        //获取工作表对象
        XSSFSheet sheet = workbook.getSheet("Sheet1");

        for (Row row : sheet) {
            for (Cell cell : row) {
                System.out.println(cell);
            }
        }

    }


    public static void main(String[] args) throws IOException {
        //获取工作薄对象
        XSSFWorkbook workbook = new XSSFWorkbook();
        //创建工作表
        XSSFSheet sheet = workbook.createSheet("test");
        //创建行对象（row）
        XSSFRow row = sheet.createRow(0);
        //创建单元格对象
        XSSFCell cell1 = row.createCell(0);
        //给单元格赋值
        cell1.setCellValue("北京市");
        //创建单元格对象
        XSSFCell cell2 = row.createCell(1);
        //给单元格赋值
        cell2.setCellValue("上海市");


        //创建行对象（row）
        XSSFRow row1 = sheet.createRow(1);
        //创建单元格对象
        XSSFCell cell3 = row1.createCell(0);
        //给单元格赋值
        cell3.setCellValue("昌平区");
        //创建单元格对象
        XSSFCell cell4 = row1.createCell(1);
        //给单元格赋值
        cell4.setCellValue("浦东区");


        //创建输出流对象
        OutputStream outputStream = new FileOutputStream("C:\\Users\\sun\\Desktop\\test.xlsx");

        //写入到文件中
        workbook.write(outputStream);

        outputStream.flush();
        outputStream.close();
        workbook.close();
    }




}
